/**
* 策略模式
*/

#include <iostream>

using namespace std;

// 抽象策略类
class Strategy {
public:
    virtual void AlgorithmInterface() = 0;
};

class StrategyA : public Strategy {
public:
    void AlgorithmInterface() {
        cout << "I am from StrategyA." << endl;
    }
};

class StrategyB : public Strategy {
public:
    void AlgorithmInterface() {
        cout << "I am from StrategyB." << endl;
    }
};

class StrategyC : public Strategy {
public:
    void AlgorithmInterface() {
        cout << "I am from StrategyC." << endl;
    }
};

class Context {
public:
    Context(Strategy *pStrategyArg) : pStrategy(pStrategyArg) {};

    void ContextInterface() {
        pStrategy->AlgorithmInterface();
    }

private:
    Strategy *pStrategy;
};

int main() {
    // Create the Strategy
    Strategy *pStrategyA = new StrategyA;
    Strategy *pStrategyB = new StrategyB;
    Strategy *pStrategyC = new StrategyC;
    Context *pContextA = new Context(pStrategyA);
    Context *pContextB = new Context(pStrategyB);
    Context *pContextC = new Context(pStrategyC);
    pContextA->ContextInterface(); // 这里就实现了使用不同的策略
    pContextB->ContextInterface();
    pContextC->ContextInterface();

    if (pStrategyA) delete pStrategyA;
    if (pStrategyB) delete pStrategyB;
    if (pStrategyC) delete pStrategyC;
    if (pContextA) delete pContextA;
    if (pContextB) delete pContextB;
    if (pContextC) delete pContextC;
}